<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>节流</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <style>
        .container {
            height: 300px;
            background-color: orange;
        }

        p {
            width: 50px;
            font-size: 100px;
            text-align: center;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="container" v-on:mousemove="move">
            <p>{{time}}</p>
        </div>
    </div>

    <script>
        /* 
        *节流: 多次调用时，以一个周期进行一次调用
        *
        * 
        */
        var app = new Vue({
            el: "#app",
            data: {
                time: 0,
                clearTimer: null,
                pre:0,
                flag:true,
            },
            methods: {
                move() {
                    // let flag =true;

                    if(this.flag){
                        this.flag = false ;
                        this.time++;
                        setTimeout(()=>this.flag=true,1000)
                    }

                    
                    // let now = Date.now()
                    // console.log("now"+now);
                    // console.log(now - this.pre);

                    // if(now - this.pre >= 1000){
                    //     setTimeout(()=>this.time++,1000);
                    //     this.pre =now;
                    //     console.log("pre"+this.pre);
                    // }
                }
            },
        });
    </script>
</body>

</html>